import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/29 20:11
 * @description 双指针 + 计数数组
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.lengthOfLongestSubstring("pwwkew");
    }
    public int lengthOfLongestSubstring(String s) {
        int[] cnt = new int[128];
        int n = s.length(), ans = 0;
        for (int i = 0, l = 0; i < n; i++) {
            char ch = s.charAt(i);
            cnt[ch]++;
            if (cnt[ch] > 1) {
                // 出现过，以i结尾的最长无重复字符串长度更新
                ans = Math.max(ans, i - l);
                // 前面计数减去
                while (l < i && s.charAt(l) != ch) {
                    cnt[s.charAt(l)]--;
                    l++;
                }
                cnt[s.charAt(l)]--;
                l++;
            }else {
                ans = Math.max(ans, i - l + 1);
            }
        }
        return ans;
    }
}
